package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

/**
 * This Class will be the JFrame that will hold 
 * and manage the gui.
 * 
 * @author Tristan D. Boucher
 * @version Commit #22 9/14/2013
 *
 */
@SuppressWarnings("serial")
public class GUIFrame extends JFrame
{
  /**
   * Reference to the current menu bar object.
   */
  private final GUIMenuBar my_menu_bar;
  
  /**
   * Memory reference to the current panel being viewed.
   * Used to swap out the old panel for the new one.
   */
  private JPanel my_current_component;
  
  /**
   * Reference to the toolbar object.
   */
  private JToolBar my_tool_bar;

  /**
   * Constructor for frame, sets size and title.
   * 
   * @param the_menu_bar The menu bar setup for this GUI. Will never
   * change from original given.
   * @param the_panel The panel given to initiate the frame. Should
   * always be the login panel.
   */
  public GUIFrame(final GUIMenuBar the_menu_bar, final JPanel the_panel)
  {
    super();
    
    my_menu_bar = the_menu_bar;
    my_tool_bar = my_menu_bar.spawnToolBar();
    my_current_component = the_panel;
    
    buildFrame(this);
    
    this.add(the_menu_bar, BorderLayout.NORTH);
    this.add(my_current_component, BorderLayout.CENTER);

    centerFrame();
  }

  /**
   * Helper method that sets up the frame.
   * 
   * @param the_frame The frame that will be built.
   * @return The frame that was passed completely built.
   */
  private JFrame buildFrame(final JFrame the_frame)
  {
    the_frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    the_frame.setTitle("M.A.R.C. Scheduler");
    the_frame.setLayout(new BorderLayout());
    the_frame.setBackground(AbstractView.BACKGROUND_COLOR);
    
    return the_frame;
  }
  
  /**
   * Swaps the current panel out with the chosen one.
   * 
   * @param the_panel The panel to replace the current
   * panel displayed.
   */
  public void swapPanel(final JPanel the_panel)
  {
    this.remove(my_menu_bar);
    my_menu_bar.rebuild();
    this.add(my_menu_bar, BorderLayout.NORTH);
    
    if (!(the_panel instanceof GUILoginPanel || the_panel instanceof GUIRegistrationPanel))
    {
      this.add(my_tool_bar = my_menu_bar.spawnToolBar(), BorderLayout.SOUTH);
    }
    else
    {
      this.remove(my_tool_bar);
    }
      
    this.remove(my_current_component);
    my_current_component.setVisible(false);
    my_current_component = the_panel;
    this.add(my_current_component);
    my_current_component.setVisible(true);
    this.setSize(my_current_component.getSize());
    
    centerFrame();
  }

  /**
   * Centers the Frame.
   */
  private void centerFrame()
  {
    final Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    this.setLocation((dim.width - my_current_component.getSize().width) / 2, 
                     (dim.height - my_current_component.getSize().height) / 2);
  }
  
  /**
   * Removes the toolBar in the event of a panel swap to a view that shouldn't
   * have the toolbar.
   */
  public void removeToolBar()
  {
    this.remove(my_menu_bar);
  }
}
